package orderController

import (
	"CMETO/models"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type OrderController struct{}

// @Summary 订单查询
// @Description 查询订单
// @Tags 订单管理
// @Accept json
// @Produce json
// @Param page query string false "页码"
// @Param pageSize query string false "尺寸"
// @Param name query string false "订单号"
// @Param status query string false 状态
// @Success 200 {object} models.ResultCommon[[]models.Order] "查询成功"
// @Success 400 {object} models.ErrorResult "查询失败"
// @Router /order [get]
// @Security JWT
func (order OrderController) QueryOrders(ctx *gin.Context) {
	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(ctx.DefaultQuery("pageSize", "10"))
	order_number := ctx.DefaultQuery("name", "")
	status := ctx.DefaultQuery("status", "1")
	numberStr := " AND 1=1"
	if order_number != "" {
		numberStr = " AND order_number = " + order_number
	}
	orderList := []models.OrderFormat{}
	models.DB.Table("orders").
		Where("orders.is_delete!=1 AND orders.status = ?"+numberStr, status).
		Limit(pageSize).Offset(pageSize * (page - 1)).
		Select("orders.*, goods.name AS goodsName,users.username AS customer").
		Joins("LEFT JOIN goods ON orders.goods_id = goods.id").
		Joins("LEFT JOIN users ON orders.customer_id = users.id").
		Scan(&orderList)
	var total int64
	models.DB.Table("orders").Where("is_delete!=1 " + numberStr).Count(&total)
	result := &models.ResultByPage[models.OrderFormat]{
		Msg:      "ok",
		Code:     "200",
		Total:    total,
		Page:     page,
		PageSize: pageSize,
		Result:   orderList,
	}
	ctx.JSON(http.StatusOK, result)
}

// @Summary 订单修改
// @Description 修改订单
// @Tags 订单管理
// @Accept json
// @Produce json
// @Param orderInfo body models.Order true "订单信息"
// @Success 200 {object} models.UpdateResult[models.Order] "更新成功"
// @fail 400 {object} models.ErrorResult "数据错误"
// @fail 401 {object} models.AddResult "修改失败"
// @Router /order [put]
// @Security JWT
func (d OrderController) UpdateOrderStatus(ctx *gin.Context) {
	var order models.Order
	// 绑定json数据
	if err := ctx.ShouldBindJSON(&order); err != nil {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "数据绑定失败",
			Code:   "400",
			Result: err,
		})
		return
	} else {
		order_old := &models.Order{}
		models.DB.Table("orders").Where("id = ?", order.Id).First(&order_old)
		order_old.Status = 0
		result := models.DB.Model(&order_old).Save(&order_old).Error
		if result != nil {
			ctx.JSON(http.StatusOK, &models.ErrorResult{
				Msg:    "更新失败",
				Code:   "400",
				Result: nil,
			})
		} else {
			ctx.JSON(http.StatusOK, &models.UpdateResult[models.Order]{
				Msg:    "更新成功",
				Code:   "200",
				Result: order,
			})
		}
	}
}

// @Summary 删除订单
// @Description 传入json，批量删除订单
// @Tags 订单管理
// @Accept json
// @Produce json
// @Param orderIds body models.DeleteParams true "订单id合集"
// @Success 200 {object} models.DeleteResult "更新成功"
// @fail 400 {object} models.ErrorResult "数据错误"
// @fail 401 {object} models.DeleteResult "删除失败"
// @Router /order [delete]
// @Security JWT
func (d OrderController) DeleteOrder(ctx *gin.Context) {
	// 绑定参数实例
	ids := &models.DeleteParams{}
	// 定义模型实例
	var order models.Order
	if err := ctx.ShouldBindJSON(&ids); err != nil {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "删除失败",
			Code:   "400",
			Result: err,
		})
	} else {
		// 假的删
		result := models.DB.Model(&order).Where("id IN (?)", ids.Ids).Update("is_delete", 1)
		if result.RowsAffected != 0 {
			ctx.JSON(http.StatusOK, &models.DeleteResult{
				Msg:    "删除成功",
				Code:   "200",
				Result: ids.Ids,
			})
		} else {
			ctx.JSON(http.StatusOK, &models.DeleteResult{
				Msg:    "删除失败",
				Code:   "400",
				Result: []int{0},
			})
		}
	}
}
